package com.first.controller;

import com.first.entity.SubjectEntity;
import com.first.service.SubjectService;
import com.first.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping("/subjectList")
    public R getSubjectList()
    {
        List<SubjectEntity> subjectList = subjectService.getSubjectList();
        if(subjectList!=null)
        {
            return R.ok().data("list",subjectList);
        }
        else
        {
            return R.error();
        }
    }

    @PostMapping("/insert/{subjectName}/{subjectInfo}/{credit}")
    public R insertSubject(@PathVariable("subjectName") String subjectName, @PathVariable("subjectInfo") String subjectInfo, @PathVariable("credit") Integer credit)
    {
        int i = subjectService.insertSubject(subjectName, subjectInfo, credit);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @PostMapping("/update/{subjectName}/{subjectInfo}/{credit}/{id}")
    public R updateSubject(@PathVariable("subjectName") String subjectName, @PathVariable("subjectInfo") String subjectInfo, @PathVariable("credit") Integer credit,@PathVariable("id") Integer id)
    {
        int i = subjectService.updateSubject(subjectName, subjectInfo, credit, id);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    public R deleteSubject(@PathVariable("id") Integer id)
    {
        int i = subjectService.deleteSubject(id);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

}
